package com.groupfour.attendancemanagementsystem.controller;

import com.groupfour.attendancemanagementsystem.entity.DO.AnswerDO;
import com.groupfour.attendancemanagementsystem.entity.DO.HomeworkDO;
import com.groupfour.attendancemanagementsystem.entity.DO.ProjectGroupDO;
import com.groupfour.attendancemanagementsystem.entity.DO.StudentDO;
import com.groupfour.attendancemanagementsystem.entity.DTO.*;
import com.groupfour.attendancemanagementsystem.entity.VO.HomeworkVO;
import com.groupfour.attendancemanagementsystem.entity.VO.RHomeworkVO;
import com.groupfour.attendancemanagementsystem.service.HomeworkService;
import com.groupfour.attendancemanagementsystem.utils.JwtHelper;
import com.groupfour.attendancemanagementsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class HomeworkManagementController {

    private HomeworkService homeworkService;

    @Autowired
    public HomeworkManagementController(HomeworkService homeworkService) {
        this.homeworkService = homeworkService;
    }


    //学生查询作业列表
    @RequestMapping("/queryHomeworkByHomeworkDTO/{pageNum}/{pageSize}")
    public Result<List<HomeworkVO>> queryHomeworkByHomeworkDTO(@RequestBody HomeworkDTO homeworkDTO,
                                                               @RequestHeader("token") String token,
                                                               @PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        String studentCode = JwtHelper.getUserCode(token);
        List<HomeworkVO> queryHomeworkByHomeworkDTOList = this.homeworkService.queryHomeworkByHomeworkDTO(studentCode,homeworkDTO,pageNum,pageSize);
        if(queryHomeworkByHomeworkDTOList.size()>0){
            return Result.ok(queryHomeworkByHomeworkDTOList);
        }else {
            return Result.fail();
        }
    }

    //学生完成作业
    @RequestMapping("/finishHomework")
    public Result finishHomework(@RequestBody AnswerDO answerDO,
                                 @RequestHeader("token") String token){
        String studentCode = JwtHelper.getUserCode(token);
        int result = homeworkService.finishHomework(studentCode,answerDO);
        if (result > 0){
            return Result.ok().message("提交成功");
        }
        return Result.fail().message("提交失败");
    }

    //接口测试通过
    //学生某一作业详情 传入学生学号、作业号
    @RequestMapping("/doSelectHomeworkDetail")
    public Result<List<HomeworkVO>> selectHomeworkDetail(@RequestBody AnswerDetailsDTO answerDetailsDTO,
                                                         @RequestHeader("token") String token){
        String studentCode = JwtHelper.getUserCode(token);
        List<HomeworkVO> list = this.homeworkService.selectHomeworkDetail(studentCode,answerDetailsDTO);
        if(list.size()>0){
            return Result.ok(list);
        }else {
            return Result.fail();
        }
    }

    //通过课程名查询单个课程的所有作业
    @RequestMapping("/selectCourseName/{courseName}/{pageNum}/{pageSize}")
    public Result selectCourseName(@PathVariable("courseName") String courseName ,
                                   @PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
        List<HomeworkVO> selectCourseNameList = homeworkService.selectHomeworkByCourseName(courseName,pageNum,pageSize);
        if (selectCourseNameList.size() > 0) {
            return Result.ok(selectCourseNameList);
        } else {
            return Result.fail();
        }
    }

     // 教师下发作业
    @PostMapping("/issueHomework")
    public Result issueHomework(@RequestBody CHomeworkDTO cHomeworkDTO,
                                @RequestHeader("token") String token) {
        String teacherCode = JwtHelper.getUserCode(token);
        List<String> students = cHomeworkDTO.getStudentName();
        List<String> groups = cHomeworkDTO.getGroupName();
        for (String studentName : students){
            int insertRes = homeworkService.insertStudentAnswer(studentName,cHomeworkDTO.getHomeworkCode());
        }
        for (String groupName : groups){
            int insertResult = homeworkService.insertGroupCodeAndHomeworkCode(groupName,cHomeworkDTO.getHomeworkCode());
        }
        int result = homeworkService.issueHomework(teacherCode,cHomeworkDTO);
        if (result > 0) {
            return Result.ok().message("作业下发成功");
        }
        return Result.fail().message("作业下发失败");
    }

    //修改作业
    @RequestMapping("/updateHomework")
    public Result updateHomework(@RequestBody HomeworkDO homeworkDO){
        int result = homeworkService.updateHomework(homeworkDO);
        if (result > 0){
            return Result.ok().message("修改成功");
        }
        return Result.fail().message("修改失败");
    }

    /**
     * @author 卑微小峰
     * @param homeworkCode
     * @return
     * @describe 教师删除作业
     */
    @RequestMapping("/deleteHomeworkByCode")
    public Result deleteHomeworkByHomeworkCode(@RequestBody String homeworkCode){
        int result = homeworkService.deleteHomeworkByHomeworkCode(homeworkCode);
        if (result > 0){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    /**
     * @author 卑微小峰
     * @param rHomeworkDTO
     * @param pageNum  当前页码
     * @return
     * @describe 教师查询作业列表
     */
    @PostMapping("/selectAllHomework/{pageNum}/{pageSize}")
    public Result selectAllHomework(@RequestBody RHomeworkDTO rHomeworkDTO,@PathVariable("pageNum") Integer pageNum,
                                    @PathVariable("pageSize") Integer pageSize) {
        List<RHomeworkVO> homeworkList = homeworkService.selectAllHomework(rHomeworkDTO,pageNum,pageSize);
        return Result.ok(homeworkList);
    }

    //教师批改作业
    @RequestMapping
    public int replyHomeworkBy(AnswerDO answerDO,
                               @RequestHeader("token") String token){
        String studentCode = JwtHelper.getUserCode(token);
        return homeworkService.replyHomeworkBy(studentCode,answerDO);
    }



    @RequestMapping("/doSelectCourseName")
    public Result<List<HomeworkDO>> selectCourseName() {
        List<HomeworkDO> list = homeworkService.selectCourseName();

        if(list.size()>0){
            return Result.ok(list);
        }else {
            return Result.fail(list);
        }
    }

    @RequestMapping("/doSelectTeacherName")
    public Result<List<HomeworkDO>> selectTeacherName() {
        List<HomeworkDO> list = homeworkService.selectTeacherName();
        if(list.size()>0){
            return Result.ok(list);
        }else {
            return Result.fail();
        }
    }

    @RequestMapping("/doSelectHomeworkTheme")
    public Result<List<HomeworkDO>> selectHomeworkTheme(){
        List<HomeworkDO> list = homeworkService.selectHomeworkTheme();
        if(list.size()>0){
            return Result.ok(list);
        }else {
            return Result.fail(list);
        }
    }

    @GetMapping("/doSelectGroupCode")
    public Result<List<ProjectGroupDO>> selectGroupCode() {
        List<ProjectGroupDO> list = homeworkService.selectGroupCode();
        if(list.size()>0){
            return Result.ok(list);
        }else {
            return Result.fail();
        }
    }

}
